/*
 * Author: Christopher Rondot
 * Contact: codiplex@gmail.com
 * Date: August 8, 2009
 * Comment: GTUG Campout Meeting
 */

package com.google.wavesurferrobot.data;

import javax.jdo.PersistenceManager;
import javax.jdo.Transaction;

import com.google.wavesurferrobot.data.objects.BlipData;
import com.google.wavesurferrobot.data.objects.WaveData;
import com.google.wavesurferrobot.data.objects.Tfidf;
import com.google.wavesurferrobot.data.objects.Token;
import com.google.wavesurferrobot.data.util.PMF;

public class StoreWaveData {
	
	public static void storeDataWave(WaveData dw) {

		PersistenceManager pm = PMF.get().getPersistenceManager();
		Transaction tx = pm.currentTransaction();
		try
		{
		    // Start the transaction
		    tx.begin();

		    // Persist it to the datastore
		    pm.makePersistent(dw);

		    // Commit the transaction, flushing the object to the datastore
		    tx.commit();
		}
		catch (Exception e)
		{
		    //e.printStackTrace();
		}
		finally
		{
		    if (tx.isActive())
		    {
		        // Error occurred so rollback the transaction
		        tx.rollback();
		    }
		    pm.close();
		}
	}	
	
	public static void storeDataBlip(BlipData dBlip) {

		PersistenceManager pm = PMF.get().getPersistenceManager();
		Transaction tx = pm.currentTransaction();
		try
		{
		    // Start the transaction
		    tx.begin();

		    // Persist it to the datastore
		    pm.makePersistent(dBlip);

		    // Commit the transaction, flushing the object to the datastore
		    tx.commit();
		}
		catch (Exception e)
		{
		    //e.printStackTrace();
		}
		finally
		{
		    if (tx.isActive())
		    {
		        // Error occurred so rollback the transaction
		        tx.rollback();
		    }
		    pm.close();
		}
	}	
	
	public static void storeTfidf(Tfidf tfidf) {

		PersistenceManager pm = PMF.get().getPersistenceManager();
		Transaction tx = pm.currentTransaction();
		try
		{
		    // Start the transaction
		    tx.begin();

		    // Persist it to the datastore
		    pm.makePersistent(tfidf);

		    // Commit the transaction, flushing the object to the datastore
		    tx.commit();
		}
		catch (Exception e)
		{
		    //e.printStackTrace();
		}
		finally
		{
		    if (tx.isActive())
		    {
		        // Error occurred so rollback the transaction
		        tx.rollback();
		    }
		    pm.close();
		}
	}	
	
	public static void storeToken(Token token) {

		PersistenceManager pm = PMF.get().getPersistenceManager();
		Transaction tx = pm.currentTransaction();
		try
		{
		    // Start the transaction
		    tx.begin();

		    // Persist it to the datastore
		    pm.makePersistent(token);

		    // Commit the transaction, flushing the object to the datastore
		    tx.commit();
		}
		catch (Exception e)
		{
		    //e.printStackTrace();
		}
		finally
		{
		    if (tx.isActive())
		    {
		        // Error occurred so rollback the transaction
		        tx.rollback();
		    }
		    pm.close();
		}
	}
	
}
